package com.wangyang.stock.business.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.wangyang.stock.business.IMerchandiseService;
import com.wangyang.stock.dao.IDao;
import com.wangyang.stock.po.Merchandise;
import com.wangyang.stock.po.MerchandiseType;
import com.wangyang.stock.po.MerchandiseUnit;
import com.wangyang.stock.vo.MerchandiseVo;

public class MerchandiseServiceImp implements IMerchandiseService {
	private IDao dao;

	public IDao getDao() {
		return dao;
	}

	public void setDao(IDao dao) {
		this.dao = dao;
	}

	public List selectMerchandise(MerchandiseVo merchandiseVo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer(
				"from Merchandise merchandise where 1=1 ");
		try {
			if (null != merchandiseVo.getMerchandiseName()) {
				if (!"".equals(merchandiseVo.getMerchandiseName())) {
					hql.append(" and merchandise.merchandiseName like '%");
					hql.append(merchandiseVo.getMerchandiseName());
					hql.append("%'");
				}
			}
			if (null != merchandiseVo.getMerchandiseNorm()) {
				if (!"".equals(merchandiseVo.getMerchandiseNorm())) {
					hql.append(" and merchandise.merchandiseNorm like '%");
					hql.append(merchandiseVo.getMerchandiseNorm());
					hql.append("%'");
				}
			}
			if (null != merchandiseVo.getMerchandiseTypeId()) {
				if (0 != merchandiseVo.getMerchandiseTypeId()) {
					hql
							.append(" and merchandise.merchandiseType.merchandiseTypeId = ");
					hql.append(merchandiseVo.getMerchandiseTypeId());
				}
			}
			if (null != merchandiseVo.getMerchandiseCid()) {
				if (!"".equals(merchandiseVo.getMerchandiseCid())) {
					hql.append(" and merchandise.merchandiseCid like '%");
					hql.append(merchandiseVo.getMerchandiseCid());
					hql.append("%'");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// System.out.println(hql.toString());
		List list = dao.select(hql.toString());
		List volist = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			Merchandise mer = (Merchandise) list.get(i);
			MerchandiseVo merVo = new MerchandiseVo();
			BeanUtils.copyProperties(mer, merVo);
			merVo.setMerchandiseTypeId(mer.getMerchandiseType().getMerchandiseTypeId());
			merVo.setMerchandiseTypeName(mer.getMerchandiseType().getMerchandiseTypeName());
			merVo.setMerchandiseUnitId(mer.getMerchandiseUnit().getMerchandiseUnitId());
			merVo.setMerchandiseUnitName(mer.getMerchandiseUnit().getMerchandiseUnitName());
			volist.add(merVo);
		}
		return volist;
	}

	public boolean updateMerchandise(MerchandiseVo merchandiseVo) {
		boolean is = false;
		Merchandise mer = (Merchandise) dao.select(
				"from Merchandise merchandise where merchandise.merchandiseId ="
						+ merchandiseVo.getMerchandiseId()).get(0);
		mer.setMerchandiseAlert(merchandiseVo.getMerchandiseAlert());

		if (dao.update(mer)) {
			is = true;
		}
		return is;
	}

	public boolean updateMerchandise2(MerchandiseVo merchandiseVo) {
		boolean is = false;
		Merchandise mer = new Merchandise();
		BeanUtils.copyProperties(merchandiseVo, mer);
		mer.setMerchandiseType(new MerchandiseType(merchandiseVo
				.getMerchandiseTypeId()));
		mer.setMerchandiseUnit(new MerchandiseUnit(merchandiseVo
				.getMerchandiseUnitId()));
		if (dao.update(mer)) {
			is = true;
		}
		return is;
	}

	public boolean createMerchandise(MerchandiseVo merchandiseVo) {
		// TODO Auto-generated method stub
		boolean is = false;
		Merchandise mer = new Merchandise();
		BeanUtils.copyProperties(merchandiseVo, mer);
		mer.setMerchandiseType(new MerchandiseType(merchandiseVo
				.getMerchandiseTypeId()));
		mer.setMerchandiseUnit(new MerchandiseUnit(merchandiseVo
				.getMerchandiseUnitId()));
		if (dao.create(mer)) {
			is = true;
		}
		return is;
	}

	public boolean deleteMerchandise(MerchandiseVo merchandiseVo) {
		// TODO Auto-generated method stub
		boolean is = false;
		Merchandise mer = new Merchandise();
		BeanUtils.copyProperties(merchandiseVo, mer);
		if (dao.delete(mer)) {
			is = true;
		}
		return is;
	}
}
